package cn.itsource.product.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.product.domain.Product;
import cn.itsource.product.domain.ProductDetail;
import cn.itsource.product.query.ProductQuery;
import cn.itsource.product.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
@Api(tags = "服务接口",description = "服务接口详细描述")
public class ProductController {
    @Autowired
    private IProductService productService;
    /**
     * 查询一个  localhost:8080/product/findone  1
     * localhost:8080/product/1
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "服务查找",notes = "根据ID查找服务,并返回详细信息")
    public Product findOne(@PathVariable("id") Long id){
        Product one = productService.findOne(id);
        return one;
    }

    /**
     * 查询一个  localhost:8080/product/findone  1
     * localhost:8080/product/1
     * @return
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "服务查找",notes = "根据ID查找服务")
    public ProductDetail findDetailByProductId(@PathVariable("id") Long id){
        ProductDetail detail = productService.findDetailByProductId(id);
        return detail;
    }

    /**
     * 查询所有localhost:8080/product
     * @return
     */
    @GetMapping
    @ApiOperation(value = "服务查找",notes = "查找所有服务")
    public List<Product> findAll(){
        List<Product> list = productService.findAll();
        return list;
    }
    @PutMapping  /*@RequestBody  接收请求体localhost:8080/product*/
    @ApiOperation(value = "添加或者修改服务",notes = "添加或者修改服务,包含详情")
    public AjaxResult addorUpdate(@RequestBody Product product){
        try {
            if (product.getId()==null){//添加
                productService.add(product);
            }else{//修改
                productService.update(product);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    @DeleteMapping("/{id}")  /*@RequestBody  接收请求体localhost:8080/product/4*/
    @ApiOperation(value = "删除服务",notes = "根据ID删除服务和详情")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            productService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    @PostMapping
    @ApiOperation(value = "分页查找服务",notes = "分页查找服务")/*localhost:8080/product*/
    public PageList<Product> queryForPage(@RequestBody ProductQuery productQuery){
        PageList<Product> pageList = productService.queryForPage(productQuery);
        return pageList;
    }

    @PostMapping("/list")
    @ApiOperation(value = "前端主站分页查找服务",notes = "前端主站分页查找服务")/*localhost:8080/product*/
    public PageList<Product> queryList(@RequestBody ProductQuery productQuery){
        // 设置查询的状态为1，即 已经上架的
        productQuery.setState(1);
        PageList<Product> pageList = productService.queryForPage(productQuery);
        return pageList;
    }

    // 批量上架
    @PostMapping("/onsale")
    public AjaxResult onSale(@RequestBody List<Long> ids) {
        try {
            // 交给server层处理 传入状态上架0 以及ids集合
            productService.onOrOffsale(1,ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  new AjaxResult().setMessage(e.getMessage());
        }
    }

    // 批量下架
    @PostMapping("/offsale")
    public AjaxResult offSale(@RequestBody List<Long> ids) {
        try {
            productService.onOrOffsale(0,ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage(e.getMessage());
        }
    }



}
